package me.jessyan.mvparms.demo.mvp.ui.adapter;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.platform.ai.control.R;


import java.util.List;

import butterknife.BindView;

import me.jessyan.mvparms.demo.mvp.model.entity.Friend;

/**
 * Created by 95 on 2017/9/5.
 */
public class FriendAdapter extends DefaultAdapter<Friend> {
    private FriendListener mListener;
    public FriendAdapter(List<Friend> friends, FriendListener mListener) {
        super(friends);
        this.mListener = mListener;
    }

    @Override
    public BaseHolder getHolder(View v, int viewType) {
        return new FriendHolder(v,mListener);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_friend;
    }

    public class FriendHolder extends BaseHolder<Friend> implements View.OnLongClickListener {
        @BindView(R.id.iv_sex)
        ImageView mSex;

        @BindView(R.id.tv_name)
        TextView mName;

        @BindView(R.id.tv_connect)
        TextView mConnect;

        @BindView(R.id.tv_author_count)
        TextView mAuthor;

        @BindView(R.id.delete)
        Button mBtnDelete;

        @BindView(R.id.author)
        Button mBtnAuthor;

        @BindView(R.id.main)
        View main;
        private FriendListener mListener;
        public FriendHolder(View itemView,FriendListener mListener) {
            super(itemView);
            this.mListener = mListener;
        }

        @Override
        public void setData(Friend data, int position) {
            switch(data.getSex()){
                case 0:
                    mSex.setImageResource(R.drawable.icon_man);
                    break;
                case 1:
                    mSex.setImageResource(R.drawable.icon_woman);
                    break;
            }
            mName.setText(data.getName());
            mConnect.setText(data.getConnect()==1?"在线":"未在线");
            mAuthor.setText(data.getIsAuthor()==1?"已授权":"未授权");
            mBtnAuthor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.author(position,data);
                }
            });
            mBtnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.delete(position,data);
                }
            });
            main.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.chat(position,data);
                }
            });
        }

        @Override
        public boolean onLongClick(View v) {
            return false;
        }
    }

    public interface FriendListener{
        void author(int position,Friend data);
        void delete(int position,Friend data);
        void chat(int position,Friend data);
    }

}
